// Reflect API Examples

// 简单的断言函数
function assert(condition, message) {
    if (!condition) {
        print(`断言失败: ${message}`);
    } else {
        print(`✓ 通过: ${message}`);
    }
}

// 1. Reflect.get - 获取对象属性
const person = { name: "Alice", age: 30 };
const nameValue = Reflect.get(person, "name");
assert(nameValue === "Alice", "Reflect.get 正确获取了属性值");

// 2. Reflect.set - 设置对象属性
const setResult = Reflect.set(person, "job", "Developer");
assert(person.job === "Developer", "Reflect.set 正确设置了属性值");
assert(setResult === true, "Reflect.set 返回了 true");

// 3. Reflect.has - 检查对象是否有某个属性
assert(Reflect.has(person, "age") === true, `Reflect.has(person, "age")`);
assert(
    Reflect.has(person, "address") === false,
    `Reflect.has(person, "address")`
);
assert(
    Reflect.has({ x: 0 }, "toString") === true,
    `Reflect.has({ x: 0 }, "toString")`
);

// 4. Reflect.deleteProperty - 删除对象属性
const deleteResult = Reflect.deleteProperty(person, "age");
assert(!("age" in person), "Reflect.deleteProperty 正确删除了属性");
assert(deleteResult === true, "Reflect.deleteProperty 返回了 true");

// 5. Reflect.construct - 使用构造函数创建对象
function User(name, age) {
    this.name = name;
    this.age = age;
}
const user = Reflect.construct(User, ["Bob", 25]);
assert(user instanceof User, "Reflect.construct 创建了正确类型的实例");
assert(
    user.name === "Bob" && user.age === 25,
    "Reflect.construct 正确传递了参数"
);

// 6. Reflect.apply - 调用函数
function greet(greeting) {
    return `${greeting}, ${this.name}!`;
}
const context = { name: "Charlie" };
const greetResult = Reflect.apply(greet, context, ["Hello"]);
assert(
    greetResult === "Hello, Charlie!",
    "Reflect.apply 正确应用了上下文和参数"
);

// 7. Reflect.defineProperty - 定义对象属性
const product = {};
const defineResult = Reflect.defineProperty(product, "price", {
    value: 100,
    writable: true,
    enumerable: true,
});
assert(product.price === 100, "Reflect.defineProperty 正确定义了属性");
assert(defineResult === true, "Reflect.defineProperty 返回了 true");

// 8. Reflect.getPrototypeOf & setPrototypeOf
const parent = {
    parentMethod() {
        return "parent method";
    },
};
const child = {};
const setProtoResult = Reflect.setPrototypeOf(child, parent);
const getProtoResult = Reflect.getPrototypeOf(child) === parent;
assert(getProtoResult, "Reflect.getPrototypeOf 返回了正确的原型");
assert(child.parentMethod() === "parent method", "子对象可以访问原型方法");
assert(setProtoResult === true, "Reflect.setPrototypeOf 返回了 true");

// 9. Reflect.ownKeys - 返回对象自身的所有属性键
const obj = { a: 1, b: 2 };
Object.defineProperty(obj, "c", { value: 3, enumerable: false });
const keys = Reflect.ownKeys(obj);
assert(keys.length === 3, "Reflect.ownKeys 返回了所有属性键");
assert(
    keys.includes("a") && keys.includes("b") && keys.includes("c"),
    "Reflect.ownKeys 包含了可枚举和不可枚举的属性"
);

// 10. Reflect 与 Proxy 结合使用
let getWasCalled = false;
const target = { message: "hello" };
const handler = {
    get(target, prop, receiver) {
        getWasCalled = true;
        return Reflect.get(target, prop, receiver);
    },
};
const proxy = new Proxy(target, handler);
const proxyMessage = proxy.message;
assert(getWasCalled, "Proxy 的 get 被调用");
assert(proxyMessage === "hello", "Proxy 正确返回了目标属性值");
